<template>
	<view>
		<view class="navBar">
			<view class="left">
				<uni-icons type="left" size="24" @click="toBack"></uni-icons>
			</view>
			<view class="center">用户注册</view>
			<view class="right"></view>
		</view>
		<view class="headText">
			<view class="register-cn">注册</view>
			<view class="register-en">Register</view>
		</view>
		<view class="registerBox">
			<view class="userInput">
				<view class="">用户名:</view>
				<input type="text" v-model="user.username" placeholder="请输入用户名" />
			</view>
			<view class="passwordInput">
				<view class="">密码:</view>
				<input type="password" v-model="user.password" placeholder="请输入密码" />
			</view>
			<view class="passwordInput">
				<view class="">重复密码:</view>
				<input type="password" v-model="user.repeatPpassword" placeholder="请输入密码" />
			</view>
			<button class="register-btn" type="primary" @click="judgment">注册</button>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				user: {
					username: '',
					password: '',
					repeatPpassword: '',
				},
			}
		},
		methods: {
			toBack() {
				uni.navigateBack({
					delta: 1,
				});
			},
			judgment() {
				if (this.user.username != '' && this.user.password != '') {
					if (this.user.password == this.user.repeatPpassword) {
						this.register();
					} else {
						uni.showToast({
							icon: "error",
							title: "重复密码错误"
						})
					}

				} else {
					uni.showToast({
						icon: "error",
						title: "不能为空"
					})
				}
			},
			register() {
				uni.request({
					url: 'http://localhost:8080/register',
					method: 'POST',
					header: {
						'content-type': 'application/x-www-form-urlencoded;charset=utf-8',
					},
					data: {
						username: this.user.username,
						password: this.user.password,
					},
					success: res => {
						uni.showToast({
							icon:"none",
							title:res.data
						})
						if(res.data== "注册成功"){
							setTimeout(function(){
								uni.navigateTo({
									url: '/pages/login/login',
								})
							},1000);
						}
					},
					fail: () => {},
					complete: () => {}
				});
			}
		}
	}
</script>

<style lang="scss">
	.navBar {
		display: flex;
		height: 100rpx;
		background-color: white;
		align-items: center;
		padding: 0 30rpx;
		/* #ifdef MP-WEIXIN */
		padding-top: 100rpx;
		/* #endif */

		.left {
			flex: 1;
		}

		.center {
			flex: 1;
			color: #646566;
			text-align: center;
			font-size: 34rpx;
			font-weight: 700;
		}

		.right {
			flex: 1;
		}
	}

	.headText {
		margin: 140rpx 30rpx;

		.register-cn {
			margin-bottom: 30rpx;
			font-size: 52rpx;
			color: #646566;
			font-weight: 700;
		}

		.register-en {
			font-size: 38rpx;
			font-weight: 400;
			color: #83838b;
		}

	}

	.registerBox {
		margin: 0 50rpx;

		.userInput,
		.passwordInput {
			display: flex;
			align-items: center;
			padding: 30rpx 0;
			border-bottom: 1rpx solid #ccc;

			view {
				flex: 1;
			}

			input {
				flex: 3;
			}
		}

		.register-btn {
			margin-top: 200rpx;
			border-radius: 50rpx;
			border: 2rpx solid #ccc;
		}
	}
</style>